//query.js
//引入高德地图sdk
var amapFile = require('../../libs/amap-wx.js'); //加载高德地图sdk
var markersData = []; //创建mark点，我们要创建多个点，所以这里是一个数组

//配置地图key 设置成全局配置 方便下面调用
var myAmapFun = new amapFile.AMapWX({
	key: 'a038f50d26bc347a0fa1b50834ad27dc'
});

Page({
	data: {
		markers: [],
		latitude: '',
		longitude: '',
		textData: {},
		scale: 16, //缩放等级 默认是16 但是坐地铁的时候 需要加大视野
		currentMenu: 1, //menu
		polyline: [] ,//路径点
		distance:0 ,//距离目标地点有多远
		zmq:[]//怎么去
	},
	onLoad: function(options) {
		//设置地图中心点
		this.setFocus();
		//传递过来的值
		//console.log(options.name);
		this.queryMap(options.name);
	},
	//设置地图中心点
	setFocus() {
		let that = this;
		wx.getLocation({
			type: 'gcj02',
			success: function(res) {
				that.setData({
					latitude: res.latitude
				});
				that.setData({
					longitude: res.longitude
				});
				that.setData({
					city: '北京市'
				});
			},
			fail: function() {
				that.setData({
					latitude: 39.909729
				});
				that.setData({
					longitude: 116.398419
				});
				that.setData({
					city: '北京市'
				});
			}
		})
	},
	//把地址显示出来
	queryMap(place) {
		let that = this;
		myAmapFun.getRegeo({
			location: place,
			iconPathSelected: '../../img/marker_checked.png', //选中时的图标
			iconPath: '../../img/marker_checked.png', //未选中时的图标
			iconWidth: 22,
			iconHeight: 32,
			success: function(data) {
				//返回附近地点的坐标
				markersData = data;
				//拿到坐标以后发给page
				that.setData({
					markers: markersData
				});
				//console.log(that.data.markers);
				//如果只有1个就直接画出路径
				if(markersData.length==1){
					that.getRoute(0)
				}
				//显示第一个数据的详情
				that.showMarkerInfo(markersData, 0);
			},
			fail: function(info) {
				wx.showModal({
					title: info.errMsg
				})
			}
		})
	},
	//把点对应的详情显示到下面
	showMarkerInfo: function(data, i) {
		var that = this;
		that.setData({
			textData: {
				name: data[i].name,
				desc: data[i].address
			}
		});
	},
	//绘制路径
	getRoute(markerId) {
		let that = this;
		wx.getLocation({
			type: 'gcj02',
			success: function(res) {
				//获取自己的位置
				that.setData({
					latitude: res.latitude
				});
				that.setData({
					longitude: res.longitude
				});
				
				let startLongitude = that.data.longitude; //获取当前位置的经度
				let startLatitude = that.data.latitude; //获取当前位置的纬度
				let origin = startLongitude + "," + startLatitude; //拼接成最终的起点数据
				
				let endLongitude = markersData[markerId].longitude; //获取当前位置的经度
				let endLatitude = markersData[markerId].latitude; //获取当前位置的纬度
				let destination = endLongitude + "," + endLatitude; //拼接成最终的终点数据
				
				myAmapFun.getDrivingRoute({
					origin: origin,
					destination: destination,
					success: function(data) {
						that.setData({
							zmq:data.paths[0].steps
						});
						var points = [];
						if (data.paths && data.paths[0] && data.paths[0].steps) {
							var steps = data.paths[0].steps;
							for (var i = 0; i < steps.length; i++) {
								var poLen = steps[i].polyline.split(';');
								for (var j = 0; j < poLen.length; j++) {
									points.push({
										longitude: parseFloat(poLen[j].split(',')[0]),
										latitude: parseFloat(poLen[j].split(',')[1])
									})
								}
							}
						}
						that.setData({
							polyline: [{
								points: points,
								color: "#0091ff",
								arrowLine:true, //显示箭头线，注意这个在pc模拟器中显示不出来，手机中可以
								width: 6
							}]
						});
						if (data.paths[0] && data.paths[0].distance) {
							that.setData({
								distance: data.paths[0].distance + '米'
							});
						}
					}
				})
			}
		})
	},
	onShareAppMessage: function () {
	    return {
	        title: '欢迎使用面试精灵！',
	        desc: '导航和日程记录，让面试简单化！',
	        path: '/pages/index/index'
	    }
	}
})
